package com.bahiaapk;

import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.bahiaapk.customize.NewsListAdapter;
import com.bahiaapk.customize.PullToRefreshListView;
import com.bahiaapk.customize.PullToRefreshListView.OnRefreshListener;
import com.bahiaapk.customize.TwitterMessageListAdapter;
import com.bahiaapk.dao.NewsDAO;
import com.bahiaapk.dao.TwitterMessageDAO;
import com.bahiaapk.dto.News;
import com.bahiaapk.dto.TwitterMessage;
import com.bahiaapk.dto.Type;
import com.bahiaapk.services.SynchronizeNews;
import com.bahiaapk.services.SynchronizeTwitter;
import com.bahiaapk.util.BahiaUtil;

/**
 * @author Igor Faria
 *
 */
public class NewsActivity extends ListActivity {
	
	private Button allNewsBTN;
	private Button popularNewsBTN;
	private Button twitterNewsBTN;
	private TextView allNewsTV;
	private TextView popularNewsTV;
	private TextView twitterNewsTV;
	private List<News> lNews;
	private List<TwitterMessage> lTwitterMessages;
	private ListAdapter adapterTwitter;
	private ListAdapter adapterListNews;
	private Type actualType;
	private Context context;
    private SynchronizeTwitter serviceTwitter;
    private SynchronizeNews serviceNews;

	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news);      
        
        allNewsBTN = (Button)findViewById(R.id.allNewsBTN);
        popularNewsBTN = (Button)findViewById(R.id.popularNewsBTN);
        twitterNewsBTN = (Button)findViewById(R.id.twitterNewsBTN);
        allNewsTV = (TextView)findViewById(R.id.allNewsTV);
        popularNewsTV = (TextView)findViewById(R.id.popularNewsTV);
        twitterNewsTV = (TextView)findViewById(R.id.twitterNewsTV);
        context = this;
        actualType = Type.All;
        listAllNews();
        // Set a listener to be invoked when the list should be refreshed.
        ((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Do work to refresh the list here.
                new GetDataTask().execute();
            }
        });


    }
    
    
    @SuppressLint("HandlerLeak")
	private class GetDataTask extends AsyncTask<Void, Void, String[]> {

        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                ;
            }
			return null;
           
        }

        @Override
        protected void onPostExecute(String[] result) {
        
        	if(actualType.equals(Type.Twitter)){
        		Handler handler = new Handler(){
	        		@Override
	     	 		public void handleMessage(Message msg){
	        			listAllTwitters();
	        		    ((PullToRefreshListView) getListView()).onRefreshComplete();
	        		}
        		};
        		serviceTwitter = new SynchronizeTwitter();
        		serviceTwitter.synchronize(context, handler);
        	}else{
        		Handler handler = new Handler(){
	        		@Override
	     	 		public void handleMessage(Message msg){
	        			if(actualType.equals(Type.All)){
	            			listAllNews();
	            		} else 	if(actualType.equals(Type.Popular)){
	            			listPopular();	
	            		}
	        		    ((PullToRefreshListView) getListView()).onRefreshComplete();
	        		}
        		};
        		serviceNews = new SynchronizeNews();
        		serviceNews.synchronize(context, handler);
        	}
            super.onPostExecute(result);
        }
    }
    
  

    private void listAllNews() {
    	
    	lNews = new ArrayList<News>();
    	lNews.clear();
    	lNews = new NewsDAO(this).findAll();    	
    	
    	
    	int sizeList = lNews.size();
    	if(lNews.size()<10){    		
    		for (int i = 0; i < BahiaUtil.minimumListSize-sizeList; i++) {
    			lNews.add(new News());
			}
    	}
    	
        adapterListNews = (ListAdapter) new NewsListAdapter(this, lNews);
	    setListAdapter(adapterListNews);
	}
    
	private void listPopular() {
		lNews = new ArrayList<News>();
    	lNews.clear();
    	lNews = new NewsDAO(this).findByRanking();    	
    	
    	int sizeList = lNews.size();
    	if(lNews.size()<10){    		
    		for (int i = 0; i < BahiaUtil.minimumListSize-sizeList; i++) {
    			lNews.add(new News());
			}
    	}    	
        adapterListNews = (ListAdapter) new NewsListAdapter(this, lNews);
	    setListAdapter(adapterListNews);
	}
    
    private void listAllTwitters() {
    	lTwitterMessages = new ArrayList<TwitterMessage>();
    	lTwitterMessages.clear();
    	lTwitterMessages = new TwitterMessageDAO(this).findAll();
		

    	int sizeList = lTwitterMessages.size();
    	if(lTwitterMessages.size()<10){    		
    		for (int i = 0; i < BahiaUtil.minimumListSize-sizeList; i++) {
    			lTwitterMessages.add(new TwitterMessage());
			}
    	}
    	adapterTwitter = (ListAdapter) new TwitterMessageListAdapter(this, lTwitterMessages);
    	setListAdapter(adapterTwitter);
	}
    
    
    
    public void changeNews(View view){
		
    	allNewsBTN.setBackgroundResource(Color.TRANSPARENT);
    	allNewsTV.setTextColor(Color.BLACK);
		
		popularNewsBTN.setBackgroundResource(Color.TRANSPARENT);
		popularNewsTV.setTextColor(Color.BLACK);
		
		twitterNewsBTN.setBackgroundResource(Color.TRANSPARENT);
		twitterNewsTV.setTextColor(Color.BLACK);
		
		switch (view.getId()) {
            case R.id.allNewsBTN: {
            	allNewsBTN.setBackgroundResource(R.drawable.img_btn_news2);
        		allNewsTV.setTextColor(Color.WHITE);
        		actualType = Type.All;
        		  listAllNews();
                break;
            }
                
            case R.id.popularNewsBTN: {
            	popularNewsBTN.setBackgroundResource(R.drawable.img_btn_news2);
            	popularNewsTV.setTextColor(Color.WHITE);
        		actualType = Type.Popular;
        		listAllNews();
                break;
            }
            
            case R.id.twitterNewsBTN: {
            	twitterNewsBTN.setBackgroundResource(R.drawable.img_btn_news2);
            	twitterNewsTV.setTextColor(Color.WHITE);
            	actualType = Type.Twitter;
            	listAllTwitters();
                break;
            }
            
           
		}
	}
    
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) { 
		super.onListItemClick(l, v, position, id);
			
		Intent intent = null;
		if(actualType.equals(Type.Twitter)){
			TwitterMessage twitterMessageSelect = (TwitterMessage) (adapterTwitter.getItem(position-1));	  
			intent = new Intent(this, TwitterDetailActivity.class);
			intent.putExtra("twitterMessage", twitterMessageSelect);

		}else{
			News newSelect = (News) (adapterListNews.getItem(position-1));
			intent = new Intent(this, NewsDetailActivity.class);
			intent.putExtra("news", newSelect);

		}	
		startActivity(intent);
		return;
	}
	
	
	
	

}